//
//  HomeFirstCell.m
//  TerritoryShopkeeper
//
//  Created by xiaowu on 2017/12/4.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "HomeFirstCell.h"

@interface HomeFirstCell ()

@end

@implementation HomeFirstCell

+ (CGFloat)getHeight {
    return (ScreenWidth * 400.0) / 375.0;
}

+ (CGFloat)getHeightWithArr:(NSArray *)arr {
    if (arr.count == 0) {
        return 0;
    } else if (arr.count <= 4) {
        return (ScreenWidth * 400.0) / 375.0 - 150 * (ScreenWidth / 375.0);
    } else {
        return (ScreenWidth * 400.0) / 375.0;
    }
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self configureUI];
    }
    return self;
}

- (void)configureUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    [self addSubview:self.firstImageView];
    [self addSubview:self.secondImageView];
    [self addSubview:self.thirdImageView];
    [self addSubview:self.fourthImageView];
    [self addSubview:self.fifthImageView];
    [self addSubview:self.sixthImageView];
    
    [self addSubview:self.clockView];
    __block typeof(self) blockSelf = self;
    self.clockView.block = ^{
        if (blockSelf.block) {
            blockSelf.block(6);
        }
    };
    
    [self addSubview:self.firstButton];
    [self addSubview:self.secondButton];
    [self addSubview:self.thirdButton];
    [self addSubview:self.fourthButton];
    [self addSubview:self.fifthButton];
    [self addSubview:self.sixthButton];
    
    
    
//    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
//    [self.firstImageView addGestureRecognizer:tapGesture];
//    [self.secondImageView addGestureRecognizer:tapGesture];
//    [self.thirdImageView addGestureRecognizer:tapGesture];
//    [self.fourthImageView addGestureRecognizer:tapGesture];
//    [self.fifthImageView addGestureRecognizer:tapGesture];
//    [self.sixthImageView addGestureRecognizer:tapGesture];

    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.firstImageView.backgroundColor = [UIColor grayColor];
    self.secondImageView.backgroundColor = [UIColor grayColor];
    self.thirdImageView.backgroundColor = [UIColor grayColor];
    self.fourthImageView.backgroundColor = [UIColor grayColor];
    self.fifthImageView.backgroundColor = [UIColor grayColor];
    self.sixthImageView.backgroundColor = [UIColor grayColor];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    CGFloat WidthRatio = ScreenWidth / 375.0;
    
    
    self.firstImageView.frame = CGRectMake(0, 0, 135 * WidthRatio - 2, 250 * WidthRatio);
    self.secondImageView.frame = CGRectMake(ScreenWidth - 240 * WidthRatio, 0, 240 * WidthRatio, 100 * WidthRatio);
    
    self.clockView.frame = CGRectMake(self.secondImageView.left+11, self.secondImageView.bottom-18-11, 11 * 6, 18);
    
    self.thirdImageView.frame = CGRectMake(self.secondImageView.left, self.secondImageView.bottom + 1, 120 * WidthRatio, 150 * WidthRatio - 1);
    self.fourthImageView.frame = CGRectMake(self.thirdImageView.right + 1, self.secondImageView.bottom + 1, 120 * WidthRatio, 150 * WidthRatio - 1);
    self.fifthImageView.frame = CGRectMake(0, self.firstImageView.bottom + 1, 188 * WidthRatio-0.5, 150 * WidthRatio);
    self.sixthImageView.frame = CGRectMake(self.fifthImageView.right + 0.5, self.fifthImageView.top, 188 * WidthRatio-0.5, 150 * WidthRatio);
    
    self.firstButton.frame = self.firstImageView.frame;
    self.secondButton.frame = self.secondImageView.frame;
    self.thirdButton.frame = self.thirdImageView.frame;
    self.fourthButton.frame = self.fourthImageView.frame;
    self.fifthButton.frame = self.fifthImageView.frame;
    self.sixthButton.frame = self.sixthImageView.frame;
    
}

- (void)tapGesture:(UITapGestureRecognizer *)gesture {
    if (self.block) {
        if (gesture.view == self.firstImageView) {
            self.block(0);
        } else if (gesture.view == self.secondImageView) {
            self.block(1);
        } else if (gesture.view == self.thirdImageView) {
            self.block(2);
        } else if (gesture.view == self.fourthImageView) {
            self.block(3);
        } else if (gesture.view == self.fifthImageView) {
            self.block(4);
        } else if (gesture.view == self.sixthImageView) {
            self.block(5);
        }
    }
}

- (void)buttonClick:(UIButton *)sender {
    if (self.block) {
        if (sender == self.firstButton) {
            self.block(0);
        } else if (sender == self.secondButton) {
            self.block(1);
        } else if (sender == self.thirdButton) {
            self.block(2);
        } else if (sender == self.fourthButton) {
            self.block(3);
        } else if (sender == self.fifthButton) {
            self.block(4);
        } else if (sender == self.sixthButton) {
            self.block(5);
        }
    } 
}

- (void)setValueForCellWithArr:(NSMutableArray <HomeClassItem *> *)arr resetTime:(CGFloat)resetTime {
    
    [self.clockView setTimerForReset:resetTime];
    
    for (NSInteger i = 0; i < arr.count; i++) {
        HomeClassItem *item = [arr objectAtIndex:i];
        
        switch (i) {
            case 0: {
                [self.firstImageView sd_setImageWithURL:[NSURL URLWithString:item.aimg] placeholderImage:PlaceholderImageBig changeContentModel:UIViewContentModeScaleAspectFill];
            } break;
            case 1: {
                [self.secondImageView sd_setImageWithURL:[NSURL URLWithString:item.aimg] placeholderImage:PlaceholderImageBig changeContentModel:UIViewContentModeScaleAspectFill];
            } break;
            case 2: {
                [self.thirdImageView sd_setImageWithURL:[NSURL URLWithString:item.aimg] placeholderImage:PlaceholderImageBig changeContentModel:UIViewContentModeScaleAspectFill];
            } break;
            case 3: {
                [self.fourthImageView sd_setImageWithURL:[NSURL URLWithString:item.aimg] placeholderImage:PlaceholderImageBig changeContentModel:UIViewContentModeScaleAspectFill];
            } break;
            case 4: {
                [self.fifthImageView sd_setImageWithURL:[NSURL URLWithString:item.aimg] placeholderImage:PlaceholderImageBig changeContentModel:UIViewContentModeScaleAspectFill];
            } break;
            case 5: {
                [self.sixthImageView sd_setImageWithURL:[NSURL URLWithString:item.aimg] placeholderImage:PlaceholderImageBig changeContentModel:UIViewContentModeScaleAspectFill];
            } break;
                
            default:
                break;
        }
    }
}

#pragma mark - Getters
- (UIImageView *)firstImageView {
    if (!_firstImageView) {
        _firstImageView = [[UIImageView alloc] init];
        _firstImageView.backgroundColor = [UIColor grayColor];
        _firstImageView.userInteractionEnabled = YES;
    }
    return _firstImageView;
}

- (UIImageView *)secondImageView {
    if (!_secondImageView) {
        _secondImageView = [[UIImageView alloc] init];
        _secondImageView.backgroundColor = [UIColor grayColor];
        _secondImageView.userInteractionEnabled = YES;
    }
    return _secondImageView;
}

- (UIImageView *)thirdImageView {
    if (!_thirdImageView) {
        _thirdImageView = [[UIImageView alloc] init];
        _thirdImageView.backgroundColor = [UIColor grayColor];
    }
    return _thirdImageView;
}

- (UIImageView *)fourthImageView {
    if (!_fourthImageView) {
        _fourthImageView = [[UIImageView alloc] init];
        _fourthImageView.backgroundColor = [UIColor grayColor];
    }
    return _fourthImageView;
}

- (UIImageView *)fifthImageView {
    if (!_fifthImageView) {
        _fifthImageView = [[UIImageView alloc] init];
        _fifthImageView.backgroundColor = [UIColor grayColor];
    }
    return _fifthImageView;
}

- (UIImageView *)sixthImageView {
    if (!_sixthImageView) {
        _sixthImageView = [[UIImageView alloc] init];
        _sixthImageView.backgroundColor = [UIColor grayColor];
    }
    return _sixthImageView;
}

- (TimeClockView *)clockView {
    if (!_clockView) {
        _clockView = [[TimeClockView alloc] initWithCornerRadius:0 andColor:[UIColor colorWithHex:0x4C4C4C alpha:1.0]];
    }
    return _clockView;
}

- (UIButton *)firstButton {
    if (!_firstButton) {
        _firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_firstButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _firstButton;
}

- (UIButton *)secondButton {
    if (!_secondButton) {
        _secondButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_secondButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _secondButton;
}

- (UIButton *)thirdButton {
    if (!_thirdButton) {
        _thirdButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_thirdButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _thirdButton;
}

- (UIButton *)fourthButton {
    if (!_fourthButton) {
        _fourthButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_fourthButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _fourthButton;
}

- (UIButton *)fifthButton {
    if (!_fifthButton) {
        _fifthButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_fifthButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _fifthButton;
}

- (UIButton *)sixthButton {
    if (!_sixthButton) {
        _sixthButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sixthButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sixthButton;
}


@end
